package com.jm.weibo.logic;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import weibo4andriod.Status;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.jm.weibo.ui.R;
import com.jm.weibo.util.DateUtil;
import com.jm.weibo.util.SubSourceUtil;

//测试Adapter
public class StatusAdapter extends BaseAdapter {
	
	public List<Status> allstatus;
	
	Context context;

	public StatusAdapter(Context con, List<Status> list) {
		this.allstatus = list;
		this.context = con;
	}

	public int getCount() {
		return allstatus.size() + 2;
	}

	public Object getItem(int position) {
		if (position == 0){
			return null;
		}else if (position == this.getCount() - 1){
			return null;
		}
		// 返回这条微薄对象
		else{
			return allstatus.get(position - 1);
		}
	}

	public long getItemId(int position) {
		if (position == 0){// 刷新
			return 0;
		} else if (position > 0 && position < this.getCount() - 1){// 返回微博的ID
			return allstatus.get(position - 1).getId();
		} else { // 选中了更多
			return -1;
		}
	}

	/**
	 * 动态给StatusAdapter添加数据
	 * @param adddata
	 */
	public void addMoreData(List<Status> adddata) {
		this.allstatus.addAll(adddata);
		this.notifyDataSetChanged();
	}

	
	private ViewHolder viewHolder = new ViewHolder();

	public View getView(int position, View convertView, ViewGroup parent) {
		if (position == 0)// 刷新
		{
			View weiboitem = LayoutInflater.from(context).inflate(
					R.layout.moreitemsview, null);
			TextView tv = (TextView) weiboitem.findViewById(R.id.tvItemContent);
			tv.setText("刷新");
			return weiboitem;
		} else if (position == this.getCount() - 1)// 更多
		{
			View weiboitem = LayoutInflater.from(context).inflate(
					R.layout.moreitemsview, null);
			TextView tv = (TextView) weiboitem.findViewById(R.id.tvItemContent);
			tv.setText("更多");
			return weiboitem;
		} else {
			View weiboitem = null;

			if (convertView != null
					&& convertView.findViewById(R.id.tvItemName) != null) {
				Log.d("getview", "doGetView-------get TextView-----------"
						+ position);
				weiboitem = convertView;
			} else {
				Log.d("getview", "doGetView-------new TextView-----------"
						+ position);
				// 把xml布局文件变成View对象
				weiboitem = LayoutInflater.from(context).inflate(
						R.layout.itemview, null);
			}
			
			this.initWeiboHoler(weiboitem);
			
			viewHolder.tvTweet_form.setText("来自:" + SubSourceUtil.getComeFromSource(allstatus.get(position-1).getSource()));
			
			viewHolder.tvItemName.setText(allstatus.get(position - 1).getUser()
					.getName());
			viewHolder.tvItemContent.setText(allstatus.get(position - 1).getText());
			
			Status status = allstatus.get(position - 1);
			if(status.getBmiddle_pic() != null && status.getBmiddle_pic().length() != 0){
				System.out.println("图片地址：" + status.getBmiddle_pic());
				viewHolder.contentPic.setVisibility(View.VISIBLE);
				
				if(MainService.allImage.get(status.getUser().getId()) != null){
					viewHolder.contentPic.setImageDrawable(MainService.allImage.get(status.getUser().getId()));
				}else{
					//设置缺省的图片
					viewHolder.contentPic.setImageResource(R.drawable.loading);
//					 if((st.getBmiddle_pic() != null) && (st.getBmiddle_pic().length() != 0)){
			    		 Map param = new HashMap();
			    		 param.put("imageUrl", status.getBmiddle_pic() );
			    		
			    		 param.put("uid", status.getUser().getId());
			    		 Task loadWeiboImageTask = new Task(Task.TASK_GET_WEIBO_IMAGE, param);
			    	     System.out.println(status.getBmiddle_pic());
			    		 MainService.newTask(loadWeiboImageTask);
//			    	 }
				}
			}
			
			Status retweetedStatus = allstatus.get(position - 1).getRetweetedStatus();
			
			// 转发内容
			if (retweetedStatus != null) {
				viewHolder.subLayout.setVisibility(View.VISIBLE);
				String txt = retweetedStatus.getText();
				viewHolder.tvItemSubContent.setText(txt);
				
				if(retweetedStatus.getBmiddle_pic() != null && retweetedStatus.getBmiddle_pic().length() != 0){
					viewHolder.subContentPic.setVisibility(View.VISIBLE);
					
					if(MainService.allImage.get(retweetedStatus.getUser().getId()) != null){
						viewHolder.subContentPic.setImageDrawable(MainService.allImage.get(retweetedStatus.getUser().getId()));
					}else{
						//设置缺省的图片
						viewHolder.subContentPic.setImageResource(R.drawable.loading);
//						 if((st.getBmiddle_pic() != null) && (st.getBmiddle_pic().length() != 0)){
				    		 Map param = new HashMap();
				    		 param.put("imageUrl", retweetedStatus.getBmiddle_pic() );
				    		
				    		 param.put("uid", retweetedStatus.getUser().getId());
				    		 Task loadWeiboImageTask = new Task(Task.TASK_GET_WEIBO_IMAGE, param);
				    	     System.out.println(status.getBmiddle_pic());
				    		 MainService.newTask(loadWeiboImageTask);
//				    	 }
					}
				
				}
			} else {
				viewHolder.subLayout.setVisibility(View.GONE);
			}
			
			viewHolder.tvItemDate.setText(DateUtil.getCreateAt(allstatus.get(
					position - 1).getCreatedAt()));
			// 是否实名认证
			if (allstatus.get(position - 1).getUser().isVerified()) {
				Log.d("ok", "ok isVerified");
				viewHolder.ivItemV.setVisibility(View.VISIBLE);
			} else {
				viewHolder.ivItemV.setVisibility(View.GONE);
			}
			// 判断有没有图片
			if (allstatus.get(position - 1).getThumbnail_pic() != null) {
				viewHolder.ivItemPic.setVisibility(View.VISIBLE);
				
			} else {
				viewHolder.ivItemPic.setVisibility(View.GONE);
			}
			// 头像
			// 如果头像已经下载
			if (MainService.allIcon.get(allstatus.get(position - 1).getUser()
					.getId()) != null) {
				viewHolder.ivItemPortrait.setImageDrawable(MainService.allIcon
						.get(allstatus.get(position - 1).getUser().getId()));
			} else {// 设定缺省的图片
				viewHolder.ivItemPortrait.setImageResource(R.drawable.portrait);
			}
			return weiboitem;
		}
	}

	private void initWeiboHoler(View weiboitem) {
		// 昵称
		viewHolder.ivItemPortrait = (ImageView) weiboitem
				.findViewById(R.id.ivItemPortrait);
		viewHolder.tvItemName = (TextView) weiboitem.findViewById(R.id.tvItemName);
		viewHolder.ivItemV = (ImageView) weiboitem.findViewById(R.id.ivItemV);
		viewHolder.tvItemDate = (TextView) weiboitem.findViewById(R.id.tvItemDate);
		viewHolder.ivItemPic = (ImageView) weiboitem.findViewById(R.id.ivItemPic);
		viewHolder.tvItemContent = (TextView) weiboitem
				.findViewById(R.id.tvItemContent);
		viewHolder.contentPic = (ImageView) weiboitem.findViewById(R.id.contentPic);
		viewHolder.subLayout = weiboitem.findViewById(R.id.subLayout);
		viewHolder.tvItemSubContent = (TextView) viewHolder.subLayout
				.findViewById(R.id.tvItemSubContent);
		viewHolder.subContentPic = (ImageView) viewHolder.subLayout
				.findViewById(R.id.subContentPic);

		viewHolder.tvTweet_form = (TextView) weiboitem.findViewById(R.id.tweet_form);
	}
}